<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="信息录入人主键" prop="infoId">
      <el-input v-model="dataForm.infoId" placeholder="信息录入人主键"></el-input>
    </el-form-item>
    <el-form-item label="分类id" prop="categoryId">
      <el-input v-model="dataForm.categoryId" placeholder="分类id"></el-input>
    </el-form-item>
    <el-form-item label="分类父id" prop="categoryPid">
      <el-input v-model="dataForm.categoryPid" placeholder="分类父id"></el-input>
    </el-form-item>
    <el-form-item label="子分类名称" prop="categoryName">
      <el-input v-model="dataForm.categoryName" placeholder="子分类名称"></el-input>
    </el-form-item>
    <el-form-item label="分类父名称" prop="categoryPname">
      <el-input v-model="dataForm.categoryPname" placeholder="分类父名称"></el-input>
    </el-form-item>
    <el-form-item label="" prop="filePath">
      <el-input v-model="dataForm.filePath" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          infoId: '',
          categoryId: '',
          categoryPid: '',
          categoryName: '',
          categoryPname: '',
          filePath: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          infoId: [
            { required: true, message: '信息录入人主键不能为空', trigger: 'blur' }
          ],
          categoryId: [
            { required: true, message: '分类id不能为空', trigger: 'blur' }
          ],
          categoryPid: [
            { required: true, message: '分类父id不能为空', trigger: 'blur' }
          ],
          categoryName: [
            { required: true, message: '子分类名称不能为空', trigger: 'blur' }
          ],
          categoryPname: [
            { required: true, message: '分类父名称不能为空', trigger: 'blur' }
          ],
          filePath: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`//tfilerelationship/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.infoId = data.tFileRelationship.infoId
                this.dataForm.categoryId = data.tFileRelationship.categoryId
                this.dataForm.categoryPid = data.tFileRelationship.categoryPid
                this.dataForm.categoryName = data.tFileRelationship.categoryName
                this.dataForm.categoryPname = data.tFileRelationship.categoryPname
                this.dataForm.filePath = data.tFileRelationship.filePath
                this.dataForm.createTime = data.tFileRelationship.createTime
                this.dataForm.updateTime = data.tFileRelationship.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`//tfilerelationship/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'infoId': this.dataForm.infoId,
                'categoryId': this.dataForm.categoryId,
                'categoryPid': this.dataForm.categoryPid,
                'categoryName': this.dataForm.categoryName,
                'categoryPname': this.dataForm.categoryPname,
                'filePath': this.dataForm.filePath,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
